package com.taiji.web.controller.workOrder;

import com.taiji.common.annotation.Log;
import com.taiji.common.config.RuoYiConfig;
import com.taiji.common.core.controller.BaseController;
import com.taiji.common.core.domain.AjaxResult;
import com.taiji.common.core.page.TableDataInfo;
import com.taiji.common.enums.BusinessType;
import com.taiji.common.utils.file.FileUploadUtils;
import com.taiji.common.utils.poi.ExcelUtil;
import com.taiji.workOrder.domain.OrderEnclosure;
import com.taiji.workOrder.service.IOrderEnclosureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 工单附件Controller
 * 
 * @author taiji
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/workOrder/OrderEnclosure")
@Api(tags = "附件管理")
public class OrderEnclosureController extends BaseController
{
    @Autowired
    private IOrderEnclosureService orderEnclosureService;

    private static String defaultBaseDir = RuoYiConfig.getProfile()+"/upload";
    /**
     * 查询工单附件列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:list')")
    @GetMapping("/list")
    @ApiOperation("查询工单附件列表")
    public TableDataInfo list(OrderEnclosure orderEnclosure)
    {
        startPage();
        List<OrderEnclosure> list = orderEnclosureService.selectOrderEnclosureList(orderEnclosure);
        return getDataTable(list);
    }

    /**
     * 导出工单附件列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:export')")
    @Log(title = "工单附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderEnclosure orderEnclosure)
    {
        List<OrderEnclosure> list = orderEnclosureService.selectOrderEnclosureList(orderEnclosure);
        ExcelUtil<OrderEnclosure> util = new ExcelUtil<OrderEnclosure>(OrderEnclosure.class);
        util.exportExcel(response, list, "工单附件数据");
    }

    /**
     * 导出工单附件列表
     *
     * @return
     */
    @Log(title = "导出工单附件", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEnclosure")
    @PermitAll
    @ApiOperation("批量导出")
    public void exportEnclosure(HttpServletResponse response, OrderEnclosure orderEnclosure) throws Exception{
        List<OrderEnclosure> list = orderEnclosureService.selectOrderEnclosureList(orderEnclosure);

        response.setContentType("application/zip");
        response.setCharacterEncoding("utf-8");

        try (ZipOutputStream zip = new ZipOutputStream(response.getOutputStream())) {
            for (OrderEnclosure item : list) {

                File file = FileUploadUtils.getAbsoluteFile(defaultBaseDir,item.getUrl().substring(item.getUrl().lastIndexOf("/") + 1));
                ZipEntry zipEntry = new ZipEntry(item.getName());
                InputStream  input = new FileInputStream(file);
                zip.putNextEntry(zipEntry);
                IOUtils.copy(input, zip);
            }
            zip.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取工单附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderEnclosureService.selectOrderEnclosureById(id));
    }

    /**
     * 新增工单附件
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:add')")
    @Log(title = "工单附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderEnclosure orderEnclosure)
    {
        return toAjax(orderEnclosureService.insertOrderEnclosure(orderEnclosure));
    }

    /**
     * 修改工单附件
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:edit')")
    @Log(title = "工单附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderEnclosure orderEnclosure)
    {
        return toAjax(orderEnclosureService.updateOrderEnclosure(orderEnclosure));
    }

    /**
     * 删除工单附件
     */
    @PreAuthorize("@ss.hasPermi('workOrder:OrderEnclosure:remove')")
    @Log(title = "工单附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderEnclosureService.deleteOrderEnclosureByIds(ids));
    }

}
